package com.nanbei.statisticsService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.internal.$Gson$Preconditions;
import com.nanbei.commonUntil.RandomUtil;
import com.nanbei.statisticsService.client.UcenterClient;
import com.nanbei.statisticsService.entity.StatisticsDaily;
import com.nanbei.statisticsService.mapper.StatisticsDailyMapper;
import com.nanbei.statisticsService.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-02-16
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

        //先根据日期将相同日期的数据进行删除
        QueryWrapper<StatisticsDaily> statisticsDailyQueryWrapper = new QueryWrapper<>();
        statisticsDailyQueryWrapper.eq("date_calculated",day);
        this.baseMapper.delete(statisticsDailyQueryWrapper);

        //每天申请的总记录数
        Integer integer = ucenterClient.countRegister(day);

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(integer);
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,300));
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,300));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,300));

        this.baseMapper.insert(statisticsDaily);

    }

    @Override
    public Map showChart(String begin, String end, String type) {

        // 先根据时间和type查询出数据
        QueryWrapper<StatisticsDaily> statisticsDailyQueryWrapper = new QueryWrapper<>();
        statisticsDailyQueryWrapper.between("date_calculated",begin,end);
        statisticsDailyQueryWrapper.select("date_calculated",type);
        List<StatisticsDaily> statisticsDailies = this.baseMapper.selectList(statisticsDailyQueryWrapper);

        //定义两个集合
        ArrayList<String> date = new ArrayList<>();
        ArrayList<Integer> count = new ArrayList<>();

        // 遍历集合
        for (StatisticsDaily s :statisticsDailies) {

            date.add(s.getDateCalculated());

            switch (type){
                case "register_num":
                        count.add(s.getRegisterNum());
                        break;
                case "login_num":
                    count.add(s.getLoginNum());
                    break;
                case "video_view_num":
                    count.add(s.getVideoViewNum());
                    break;
                case "course_num":
                    count.add(s.getCourseNum());
                    break;
                default:
                    break;
            }

        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("dateList",date);
        hashMap.put("countList",count);

        return hashMap;
    }
}
